﻿using System;
using System.Collections.Generic;
 
using System.Text;
using GGSharp.Packets;
using GGSharp.Structures;

namespace GGSharp
{
    public class GGPubDir
    {
        public delegate void ResponseReceivedDelegate(object sender, PubDirResponse50Packet response);

        public event ResponseReceivedDelegate ResponseReceived;

        protected GGSender _sender;
        protected GGReceiver _receiver;

        public GGPubDir(GGSender sender, GGReceiver receiver)
        {
            _sender = sender;
            _receiver = receiver;

            _receiver.PacketReceived += new GGReceiver.PacketReceivedDelegate(_receiver_PacketReceived);
        }

        void _receiver_PacketReceived(object sender, HeaderPacket packet)
        {
            switch (packet.Type)
            {
                case PacketType.GG_PUBDIR50_REPLY:
                    OnResponseReceived(packet as PubDirResponse50Packet);
                    break;
            }
        }

        /// <summary>
        /// Reads our Public Directory profile from server.
        /// </summary>
        /// <returns></returns>
        public int Read()
        {
            PubDirRequest50Packet pubDirRequest = new PubDirRequest50Packet();
            pubDirRequest.QueryType = PubDirType.Read;
            _sender.Send(pubDirRequest);
            return pubDirRequest.Sequence;
        }

        /// <summary>
        /// Reads the specified numbers Public Directory informations.
        /// </summary>
        /// <param name="number">The number.</param>
        /// <returns></returns>
        public int Read(int number)
        {
            PubDirContactSearch query = new PubDirContactSearch();
            query.Number = number;
            return Search(query);
        }

        /// <summary>
        /// Searches Public Directory with specified query.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <returns></returns>
        public int Search(PubDirContactSearch query)
        {
            PubDirRequest50Packet pubDirRequest = new PubDirRequest50Packet();
            pubDirRequest.QueryType = PubDirType.Search;
            pubDirRequest.Query = query;
            _sender.Send(pubDirRequest);
            return pubDirRequest.Sequence;
        }

        /// <summary>
        /// Writes our Public Directory profile to the server.
        /// </summary>
        /// <param name="query">The profile data.</param>
        /// <returns></returns>
        public int Write(PubDirPersonal query)
        {
            PubDirRequest50Packet pubDirRequest = new PubDirRequest50Packet();
            pubDirRequest.QueryType = PubDirType.Write;
            pubDirRequest.Query = query;
            _sender.Send(pubDirRequest);
            return pubDirRequest.Sequence;
        }

        protected virtual void OnResponseReceived(PubDirResponse50Packet response)
        {
            if (ResponseReceived != null)
                ResponseReceived(this, response);
        }
    }
}
